/**══════════════════════════════════╗
*作    者：songbaobao                                                  ║
*职    业：我以我血荐轩辕                                              ║
*CSND地址：https://blog.csdn.net/weixin_43949535                       ║
**GitHub ：https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE  ：https://gitee.com/lucky912_admin/code-up_-pat               ║
*═══════════════════════════════════╣
*创建时间：                                                           
*功能描述：                                                            
*                                                                      
*                                                                      
*═══════════════════════════════════╣
*结束时间:                                                           
*═══════════════════════════════════╝
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//              西南\./联大               \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string.h>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
using namespace std;

#if PA1025
struct Student
{
	char registration_number[14] = { 0 };// 考试证号
	int score;// 分数
	int location_number;// 考场号
	int local_rank;// 本考场内的排名
}student[30001];

bool mycompare(Student &a, Student &b)//比较函数
{
	if (a.score != b.score)return a.score > b.score;//先按分数从高到低 排序
	else return strcmp(a.registration_number, b.registration_number) < 0;//分数相同   按照准考证号从小到大排序
}
#endif

#if C
struct Student
{
	int id;
	char name[9];
	int score;

	Student(int id1, char name1[], int score1)
	{
		id = id1;
		strcpy(name, name1);
		score = score1;
	}
};

// 在codeup上面使用 引用会有报错 到时候去掉 & 即可
bool cmp1(Student& a, Student& b)
{
	return a.id < b.id;
}

bool cmp2(Student& a, Student& b)
{
	int result = strcmp(a.name, b.name);
	if (result != 0)
		return result < 0;
	return a.id < b.id;
}

bool cmp3(Student& a, Student& b)
{
	if (a.score != b.score)
		return a.score < b.score;
	return a.id < b.id;
}
#endif 

struct Animal
{
	int weight;
	char color[11];

	Animal(int w, char c[])
	{
		weight = w;
		strcpy(color, c);
	}
};

bool cmpAnimal(Animal a, Animal b)
{
	return a.weight > b.weight;
}

#if I
struct Student
{
	char id[21];
	int m;
	int score;

	Student(char id1[], int m1, int score1)
	{
		strcpy(id, id1);
		m = m1;
		score = score1;
	}
};

bool cmpStudentScore(Student& a, Student& b)
{
	if (a.score != b.score)
		return a.score > b.score;
	return a.id < b.id;
}
#endif

int main()
{
#if PA1025
	int N, total = 0, location_number = 1;
	scanf("%d", &N);
	while (N--)
	{
		int K, local_rank = 1, localNum;
		scanf("%d", &K);
		localNum = K;
		while(localNum--)
		{
			scanf("%s%d", student[total].registration_number, &student[total].score);
			student[total].location_number = location_number;
			total++;
		}
		sort(student + total - K, student + total, mycompare);
		student[total - K].local_rank = local_rank;
		for (int i = total - K + 1; i < total; ++i)
		{
			if (student[i].score != student[i - 1].score)
			{
				local_rank = i + 1 - (total - K);
				student[i].local_rank = local_rank;
			}
			else
			{
				student[i].local_rank = student[i - 1].local_rank;
			}
		}
		location_number++;
	}
	
	sort(student, student + total, mycompare);
	printf("%d\n", total);

	int finalrank = 1;
	printf("%s %d %d %d\n", student[0].registration_number, finalrank,
		student[0].location_number, student[0].local_rank);
	for (int i = 1; i < total; ++i)
	{
		if (student[i].score != student[i - 1].score)
		{
			finalrank = i + 1;
		}
		printf("%s %d %d %d\n", student[i].registration_number, finalrank,
				student[i].location_number, student[i].local_rank);
	}
#endif

#if A
	int N;
	while (~scanf("%d", &N))
	{
		vector<int>myvec;
		int val;
		for (int i = 0; i < N; ++i)
		{
			cin >> val;
			myvec.push_back(val);
		}

		sort(myvec.begin(), myvec.end());
		for (vector<int>::iterator it = myvec.begin(); it != myvec.end(); ++it)
		{
			cout << *it << " ";
		}
		cout << endl;
	}
#endif

#if B
	int N;
	while (~scanf("%d", &N))
	{
		vector<int>myvec;
		int val;
		for (int i = 0; i < N; ++i)
		{
			cin >> val;
			myvec.push_back(val);
		}

		sort(myvec.begin(), myvec.end());

		cout << myvec[N - 1] << endl;
		int i = 0;
		for (vector<int>::iterator it = myvec.begin(); it != myvec.end() && i < N - 1; ++it, ++i)
		{
			cout << *it << " ";
		}
		if (N == 1)
			cout << -1;
		cout << endl;
	}
#endif

#if C
	int N, C, count = 0;
	while (~scanf("%d%d", &N, &C), N != 0)
	{
		vector<Student>myvec;
		int id, score; char name[9] = { 0 };
		for (int i = 0; i < N; ++i)
		{
			cin >> id >> name >> score;
			myvec.push_back(Student(id, name, score));
		}

		if (1 == C)
		{
			sort(myvec.begin(), myvec.end(), cmp1);
		}
		else if (2 == C)
		{
			sort(myvec.begin(), myvec.end(), cmp2);
		}
		else if (3 == C)
		{
			sort(myvec.begin(), myvec.end(), cmp3);
		}

		// cout << "Case " << C << ":" << endl;

		printf("Case %d:\n", ++count);
		for (vector<Student>::iterator it = myvec.begin(); it != myvec.end(); ++it)
		{
			// cout << setfill('0') << setw(6) << it->id << " " << it->name << " " << it->score << endl;
			printf("%06d %s %d\n", it->id, it->name, it->score);
		}
	}
#endif 

#if D
	char str[201] = { 0 };
	while (NULL != gets_s(str))
	{
		vector<char>myvec;
		int len = strlen(str);
		for (int i = 0; i < len; ++i)
		{
			myvec.push_back(str[i]);
		}
		sort(myvec.begin(), myvec.end());

		for (vector<char>::iterator it = myvec.begin(); it != myvec.end(); ++it)
		{
			cout << *it;
		}
		cout << endl;
	}
#endif

#if F
	int N;
	while (~scanf("%d", &N))
	{
		int w; char color[11] = { 0 };
		
		vector<Animal>myvec;

		for (int i = 0; i < N; ++i)
		{
			cin >> w >> color;
			myvec.push_back(Animal(w, color));
		}
		sort(myvec.begin(), myvec.end(), cmpAnimal);

		for (vector<Animal>::iterator it = myvec.begin(); it != myvec.end(); ++it)
		{
			cout << it->color << endl;
		}
	}
#endif

#if G
	int N;
	while (~scanf("%d", &N), N != 0)
	{
		int val, mid;
		if (1 == N)
		{
			cin >> val;
			cout << val << endl;

			continue;
		}

		vector<int>myvec;
		for (int i = 0; i < N; ++i)
		{
			cin >> val;
			myvec.push_back(val);
		}
		sort(myvec.begin(), myvec.end());

		mid = N / 2;

		if (N % 2 == 1)
		{
			cout << myvec[mid] << endl;
		}
		else
		{
			cout << (myvec[mid - 1] + myvec[mid]) / 2 << endl;
		}
	}
#endif

#if H

	int array[10] = { 0 };

	while (cin >> array[0] >> array[1] >> array[2] >> array[3] >> array[4] >> array[5] >>
		array[6] >> array[7] >> array[8] >> array[9])
	{
		vector<int>myvec1;
		vector<int>myvec2;

		for (int i = 0; i < 10; ++i)
		{
			if (array[i] % 2 == 1)
				myvec1.push_back(array[i]);
			else
				myvec2.push_back(array[i]);
		}
		sort(myvec1.begin(), myvec1.end());
		sort(myvec2.begin(), myvec2.end());

		for (vector<int>::reverse_iterator it = myvec1.rbegin(); it != myvec1.rend(); ++it)
		{
			cout << *it << " ";
		}
		for (vector<int>::iterator it = myvec2.begin(); it != myvec2.end(); ++it)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

#endif

#if I
	int N, M, G;

	while (~scanf("%d", &N), N != 0)
	{
		scanf("%d%d", &M, &G);

		vector<int>perScore;
		vector<Student>perStudent;
		int perscore;
		int num = 0;
		for (int i = 0; i < M; ++i)
		{
			cin >> perscore;
			perScore.push_back(perscore);
		}

		for (int i = 0; i < N; ++i)
		{
			char id[21];
			int m;
			int totalScore = 0;

			cin >> id >> m;

			for (int i = 0; i < m; ++i)
			{
				int val;
				cin >> val;
				
				totalScore += perScore[val - 1];
			}

			perStudent.push_back(Student(id, m, totalScore));
		}
		
		sort(perStudent.begin(), perStudent.end(), cmpStudentScore);

		for (vector<Student>::iterator it = perStudent.begin(); it != perStudent.end(); ++it)
		{
			if (it->score >= G)
				num++;
		}
		cout << num << endl;
		for (vector<Student>::iterator it = perStudent.begin(); it != perStudent.end(); ++it)
		{
			if (it->score >= G)
				cout << it->id << " " << it->score << endl;
		}
	}
#endif

#if E
	int m;
	while (~scanf("%d", &m))
	{
		vector<vector<int>>myvec;
		vector<int>result;
	
		int val;
		for (int i = 0; i < m; ++i)
		{
			int sum = 0;
			vector<int>vec;
			for (int j = 0; j < m; ++j)
			{
				cin >> val;
				sum += val;
				vec.push_back(val);
			}
			result.push_back(sum);
			myvec.push_back(vec);
		}

		int sum1 = 0;
		int sum2 = 0;
		for (int i = 0; i < m; ++i)
		{
			int sum3 = 0;
			for (int j = 0; j < m; ++j)
			{
				if (i == j)
				{
					sum1 += myvec[i][j];
				}
				if (m == (i + j + 1))
				{
					sum2 += myvec[i][j];
				}
					
				sum3 += myvec[j][i];
			}
			
			result.push_back(sum3);
		}
		result.push_back(sum1);
		result.push_back(sum2);

		sort(result.begin(), result.end());

		for (vector<int>::reverse_iterator it = result.rbegin(); it != result.rend(); ++it)
		{
			cout << *it << " ";
		}
		cout << endl;

	}
#endif

	return 0;
}
/**
*备用注释：
*
*
*
*/
